<template>
  <dtDialog
    title="日志清理"
    :visible.sync="dialogVisible"
    :is-loading="loading"
    @closeDialog="taskResetTemp()"
    @comfirmBtn="onSubmit"
  >
    <el-form
      slot="content"
      ref="formRef"
      :model="log"
      :rules="rules"
      label-position="right"
      label-width="101px"
    >
      <el-form-item label="任务名称" prop="timer">
        <el-select v-model.trim="timer" placeholder="请选择" style="width: 100%"
                   value-key="timerId" @change="changeTimer">
          <el-option
            v-for="item in taskList"
            :key="item.timerId"
            :label="item.timerName"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="任务CLASS" prop="timerClass">
        <el-input v-model.trim="log.timerClass" disabled />
      </el-form-item>
      <el-form-item label="清理方式" prop="clearWay">
        <el-select v-model.trim="log.clearWay" filterable placeholder="请选择"
                   style="width: 100%">
          <el-option v-for="item in clearWayList" :key="item.value" :label="item.label"
                     :value="item.value" />
        </el-select>
      </el-form-item>
    </el-form>
  </dtDialog>
</template>
<script>
import taskLogApi from '@/framework/api/taskLog'

export default {
  data() {
    const timerValidate = (rule, value, callback) => {
      if (!this.timer.timerName) {
        callback(new Error('请选择任务名称'))
      } else {
        callback()
      }
    }
    return {
      timer: {},
      dialogVisible: false,
      form: {},
      log: {
        timerName: '',
        timerClass: '',
        clearWay: ''
      },
      taskList: [],
      rules: {
        timer: [{
          required: true,
          validator: timerValidate,
          trigger: 'change'
        }],
        timerClass: [{
          required: true,
          message: '请先选择任务名称',
          trigger: 'blur'
        }
        ],
        clearWay: [{
          required: true,
          trigger: 'change',
          message: '请选择清理方式'
        }]
      },
      // 清理方式
      clearWayList: [
        {
          value: '1',
          label: '一个月'
        },
        {
          value: '3',
          label: '三个月'
        },
        {
          value: '6',
          label: '六个月'
        },
        {
          value: '12',
          label: '一年'
        },
        {
          value: '0',
          label: '所有'
        }
      ],
      loading: false
    }
  },
  methods: {
    openDialog() {
      this.dialogVisible = true
      this.getTaskName()
    },
    changeTimer(e) {
      this.log.timerClass = e.actionClass
    },
    // 获取任务名称
    getTaskName() {
      taskLogApi.getTaskName().then((res) => {
        if (res.success) {
          this.taskList = res.data.rows
          const add = {
            actionClass: '全部',
            timerName: '全部',
            timerId: null
          }
          this.taskList.unshift(add)
        }
      })
    },
    onSubmit() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$dtMessageBox({ title: '提示', message: '此操作将清空日志, 是否继续?', type: 'warning' }).then(() => {
            if (!this.timer.timerId) {
              this.log.timerName = ''
              this.log.timerClass = ''
            } else {
              this.log.timerName = this.timer.timerName
              this.log.timerClass = this.timer.actionClass
            }
            try {
              taskLogApi.deleteLog(this.log).then((resp) => {
                this.dialogVisible = false
                this.$dtMessage({
                  type: 'success',
                  message: `${resp.message}`
                })
                this.$parent.getList()
              })
              this.loading = false
            } catch (error) {
              this.loading = false
            }
          })
          this.loading = false
        }
      })
    },
    taskResetTemp() {
      this.$refs.formRef.resetFields()
      this.timer = {}
    }
  }
}
</script>
